<?php
/**
 *
 *@package 
 *@copyright Copyright (c) 2012-2014 Tuniu.com
 *@author huxiaomin 2012
 *@version 1.0
 */
            /**
             * 公共缓存key方法
             * 增加全局缓存版本号与局部版本号。//TODO
             * @param string $mix_value
             * @param string or object $class
             * @param string $func
             * @return string or ''
             */
            function gCacheKey($mix_value,$class='',$func='',$num=1,$self_key=''){
                if (is_array($mix_value)){
                    $mix_value = json_encode($mix_value);
                }
                
                if ($class){
                    if (is_string($class) && !class_exists($class)){
                    	$mod = new TMod();
                    	$table = new TTable();
                        if (!$mod->load($class) && ! $table->load($class)){
                        	$str = debug_backtrace();
                            $str = $str[$num];
                            TLog::logDb("gCacheKey class not exists :{$class} detail:".json_encode($str),
                                        'gCacheKey','gCacheKey','sys');
                            return '';
                        }
                    }
                    
                    $reflection = new ReflectionClass($class);
                    $file_name = $reflection->getFileName();
		            $file_name = basename($file_name);
		            $class_name = $reflection->getName();
		            $key = "{$file_name}:{$class_name}.{$func}?";
                }
                else {
                $str = debug_backtrace();
                $str = $str[$num];
                $key = basename($str['file']).':'.$str['class'].
                       '.'.$str['function'].'?';
                }
                $key .= $self_key.'?';
                //return $key.$mix_value;
                require_once TTABLE_DIR.'DcacheKey'.TTABLE_SUFFIX;
                $dcache_key = new DcacheKeyTable();
                $dcache_key->db = DDb::Dao();
                $single_key = $dcache_key->getSingleKeyVersion($key);
                $all_key    = $dcache_key->getAllKeyVersion();
                return "{$key}{$single_key}?{$all_key}?{$mix_value}";
                
            }

            /**
             * 抛出异常
             * @param $boolean
             */
            function gthrow($boolean=1){
                if ($boolean){
                    throw  new Exception();
                }
            }
            
            
            function gprint_r($value,$file=''){
                if (DEBUG_PRINT){
                	$time = gtime().'/'.date('Y-m-d H:i:s');
                	$str = debug_backtrace();
                    $str_new = array('print_r'=>print_r($value,1),'backtrace'=>$str[1]);
                	echo "//-->begin gprint_r : {$time} <br/>\n ";
                    print_r($str_new);
                    $time = gtime().'/'.date('Y-m-d H:i:s');
                    echo "<br/>\n//-->end gprint_r : {$time} <br/>\n";
                }
            }
            
            /**
             * 全局的存储数据
             * @param $key
             * @param $value
             */
            function gdata($key = '', $value = ''){
                static $gdata = array();
                $count = count(func_get_args());
                
                if ( $count == 0){
                    return $gdata;
                }
                else if ($count == 1){
                    return $gdata[$key];
                }
                else {
                    return $gdata[$key] = $value;
                }
                
                
            }
            
            
            /**
             * 用于规范输出
             * @param $success bool      标记
             * @param $data mix       数据（提供返回）
             * @param $msg  string    描述
             * @param $in   array     数据 （ 提供错误数据）
             */
             function gout($success=false,$data=array(),$msg='',$in=array())
             {
                $success ? $success = true : $success = false;
                return array(
                            'success' => $success,
                            'data' => $data,
                            'msg'  => trim($msg),
                            'in'   => print_r($in,true),
                        );
             }
             
             
             function goutJson($success=false,$data=array(),$msg='',$in=array())
             {
                $success ? $success = true : $success = false;
                return json_encode(array(
                            'success' => $success,
                            'data' => $data,
                            'msg'  => trim($msg),
                            'in'   => print_r($in,true),
                        ));
             }
             
             /**
              * 
              * @param $instance   object || string 实例或者类名
              * @param $method     string 方法名
              * 
              */
             function gmethod($instance,$method)
             {
                is_object($instance) || $instance = new $instance();
                //判断是否有接口
                $arg_list = func_get_args();
                unset($arg_list[0]);
                unset($arg_list[1]);
                return call_user_func_array( array( $instance ,$method) , $arg_list);
             }
             
             /**
              *用于解析异常
              *@param $e Exception
              *@param $flag bool
              */
             function gcatch($e,$flag=0)
             {
                $str = "Exception code:". $e->getCode() ;
                $str .= "Exception message: ". nl2br($e->getMessage()) ;
                $str .= "Thrown by: '". $e->getFile() ."'";
                $str .= "on line: '". $e->getLine() ."'.";
                $str .= "Stack trace:";
                $str .= nl2br($e->getTraceAsString());
                if ($flag)
                {
                    echo $str;exit;
                }
                return $str;
             }
             
             /**
              * 用于解析debug
              * @param $flag bool
              */
             function gdebug($flag=0)
             {
                $str = debug_backtrace();
                if ($flag)
                {
                    print_r($str);exit;
                }
                return $str;
             }
             /**
              * 用于解析debug
              * @param $flag bool
              */
             function gdebug_db($msg='',$flag=0)
             {
                $str = debug_backtrace();
                $str_new = array($msg,$str[2],$str[3]);
                if ($flag)
                {
                    print_r($str_new);exit;
                }
                return $str_new;
             }
             
             /**
              * @param array $from 需要处理的数组
              * @param array $to   处理成的数据结构带默认值
              * @example $from = array('1'=>3,'2'=>3) $to = array('1'=>1,'3'=>2) result array('1'=>3,'3'=>2)
              */
             function garray($from ,$to)
             {
                $return = array();
                foreach ((array)$to as $key=>$v)
                {
                    if(array_key_exists($key,$from))
                    {
                        $return[$key] = $from[$key];
                    }else {
                        $return[$key] = $v;
                    }
                }
                return $return;
             }
             
             /**
              * 时间
              */
             function gtime()
             {
             	    return microtime(1);
                    list($usec, $sec) = explode(" ", microtime());
                    return ((float)$usec + (float)$sec);
             }
             
             /**
              * array类型转换成其他
              */
             function gdataType($data, $datatype = 'txt'){
                if (is_string($data)){
                    return $data;
                }
                
                if (is_array($data)){
                    return json_encode($data);
                }
             }